<script type="text/javascript" src="<?=base_url()?>resource/js/layer/layer.js"></script>
<script type="text/javascript" src="<?=base_url()?>resource/js/layer/extend/layer.ext.js"></script>
<script type="text/javascript">
    $(function(){
        var msg= '<?=$msg?>';
        if(msg){
            alert(msg);
        }
    });
    /**
     * 设置账号信息
     * @param id
     * @constructor
     */
    function SetAccount(id){
        $('#pay_shop_id').empty();
        $('#pay_shop_id').val('');
        layer.open({
            type: 1,
            title: '设置账户关联----更新的信息将会推送至 TomRepair',
            area: ['900px', '650px'], //宽高
            content: $('#infoView')
        });
        var index = layer.load(2);
        $.post("<?=blang_url('ajax/ajax/TrAccountInfo')?>",{'id':id},function(data){
            if(data) {
                layer.close(index);
                var obj=JSON.parse(data);
                var accountInfo=obj['accountInfo'];
                var shopData=obj['shopData'];
                var branchHtml='';
                var radioCk=[];
                switch (accountInfo['role']){
                    case '1':
                        radioCk[1]='checked';
                        break;
                    case '2':
                        radioCk[2]='checked';
                        break;
                    case '100':
                        radioCk[100]='checked';
                        break;
                    default :
                        radioCk[1]='checked';
                        break;
                }
                $('#roleTd').html("<input type='radio' name='role' "+radioCk[1]+" value='1' onclick='RadioChange("+id+",1)' id='role1'>电商客户"+
                                  "<input type='radio' name='role' "+radioCk[2]+" value='2' onclick='RadioChange("+id+",2)' id='role2'>品牌客户"+
                                  "<input type='radio' name='role' "+radioCk[100]+" value='100' onclick='RadioChange("+id+",100)' id='role2'>管理员");
                $('#name').html(accountInfo['name']);
                $('#id').val(id);
                $('#email').html(accountInfo['email']);
                $('#company').html(accountInfo['company']);
                $('#tr_id').val(accountInfo['tr_id']);
                for(var i=0;i<shopData.length;i++){
                    branchHtml+="<div class='checkboxDiv' title='"+shopData[i]['name']+"'>"+
                        "<input type='checkbox' name='branch[]' "+shopData[i]['ck']+" onclick='CheckboxChange(this)'  value='"+shopData[i]['id']+"_/"+shopData[i]['name']+"'>"+shopData[i]['name']+shopData[i]['y_n']+
                        "</div>";
                }
                //店铺列表
                $('#branch').html(branchHtml);
                //付款店铺下拉列表
                $('input[name="branch[]"]:checked').each(function(){
                    var val=$(this).val();
                    var shopId=val.split('_/');
                    var pay_shop_id_Html="<option value='"+shopId[0]+"'>"+shopId[1]+"</option>";
                    $('#pay_shop_id').append(pay_shop_id_Html);
                });
                //设置付款店铺选中项
                $('#pay_shop_id').val(accountInfo['pay_shop_id']);
                //设置权限选中项
                $(accountInfo['permission'].split(",")).each(function (i,dom){
                    $(":checkbox[value='"+dom+"']").prop("checked",true);
                });
            }
        });
    }

    /**
     * 账号类型切换 (大量重复代码 有时间优化)
     * @param id
     * @param type_id
     * @constructor
     */
    function RadioChange(id,type_id){
        var index=layer.load(2);
        $('#pay_shop_id').empty();
        $('#pay_shop_id').val('');
        $.post("<?=blang_url('ajax/ajax/TrAccountInfo')?>",{'id':id,'type_id':type_id},function(data){
            if(data){
                layer.close(index);
                var obj=JSON.parse(data);
                var shopData=obj['shopData'];
                var branchHtml='';
                for(var i=0;i<shopData.length;i++){
                    branchHtml+="<div class='checkboxDiv' title='"+shopData[i]['name']+"'>"+
                    "<input type='checkbox' name='branch[]' "+shopData[i]['ck']+" onclick='CheckboxChange(this)' value='"+shopData[i]['id']+"_/"+shopData[i]['name']+"'>"+shopData[i]['name']+shopData[i]['y_n']+
                    "</div>";
                }
                $('#branch').html(branchHtml);
                //付款店铺下拉列表
                $('input[name="branch[]"]:checked').each(function(){
                    var val=$(this).val();
                    var shopId=val.split('_/');
                    var pay_shop_id_Html="<option value='"+shopId[0]+"'>"+shopId[1]+"</option>";
                    $('#pay_shop_id').append(pay_shop_id_Html);
                });
                //设置付款店铺选中项
                $('#pay_shop_id').val(accountInfo['pay_shop_id']);
            }
        });
    }
    /**
     * 选中赋值
     * @param obj
     * @constructor
     */
    function CheckboxChange(obj){
        var val=$(obj).val();
        var ck=$(obj).is(':checked') ;
        var shopId=val.split('_/');
        var pay_shop_id_Html="<option value='"+shopId[0]+"'>"+shopId[1]+"</option>";
        if(ck) {
            $('#pay_shop_id').append(pay_shop_id_Html);
        }else{
            $("#pay_shop_id option[value='"+shopId[0]+"']").hide();
            $("#pay_shop_id option[value='"+shopId[0]+"']").remove();
        }
    }
    /**
     * 提交验证
     * @constructor
     */
    function Sub(){
        var branch=$("input[name='branch[]']").is(':checked');
        var permission=$("input[name='permission[]']").is(':checked');
        if(!branch){
            layer.tips('至少选择一个店铺', '#branch', {
                tips: [1, '#0FA6D8'] //还可配置颜色
            });
            return false;
        }
        if(!permission){
            layer.tips('至少选择一种权限', '#permission', {
                tips: [1, '#0FA6D8'] //还可配置颜色
            });
            return false;
        }
        var index=layer.load(2);
    }
</script>
<style>
    .checkboxDiv{
        background-image: url("http://oms.tomrepair.es/resource/images/m17_u3.png");
        width: 150px;
        height: 30px;
        line-height:30px;
        overflow:hidden;
        text-align: center;
        float: left;
        margin-left: 8px;
        margin-right: 8px;
        margin-top: 5px;
        margin-bottom: 5px;
    }
</style>

<!--菜单 start-->
<?php $this->load->view('defaults/common/left');?>
<!--菜单 end-->
<div id="content">
    <div class="page-header">
        <div class="container-fluid">
          <h1><?=lang('menu_yhgl')?></h1>
          <?=$this->breadcrumb->generateHTML();?>
        </div>
    </div>
    <div class="container-fluid">
        <div class="panel panel-default">
          <div class="panel-heading">
            <h3 class="panel-title"><i class="fa fa-list"></i>Tr账户列表</h3>
          </div>
          <div class="panel-body">
            <div class="well" style="padding:0 19px;">
              <div class="row">
                <form action="<?=blang_url('users/member/TrAccountList')?>" method="post" name="searchform">
                    <div class="col-sm-4">
                        <div class="form-group">
                            <label class="control-label">账户名/邮箱/公司</label>
                            <input class="form-control" type="text" placeholder="账户名/邮箱/公司" size="80" name="keyword">
                        </div>
                    </div>
                    <div class="col-sm-8">
                        <div class="form-group" style="padding-top:37px;">
                            <input class="btn btn-primary" type="submit" value=" 查 询 ">
                        </div>
                    </div>
                </form>
              </div>
            </div>

            <div class="table-responsive">
                <table class="table table-bordered table-hover oms-table">
                  <thead>
                    <tr>
                        <th class="text-left">ID</th>
                        <th class="text-left">账户名</th>
                        <th class="text-left">邮箱</th>
                        <th class="text-right">角色</th>
                        <th class="text-right">公司</th>
                        <th class="text-right">操作</th>
                    </tr>
                  </thead>
                  <tbody>
                    <?php if($accountList): foreach ($accountList as $key=>$row):?>
                    <tr>
                        <td class="text-left"><?=$row['tr_id']?></td>
                        <td class="text-left"><?=$row['name']?></td>
                        <td class="text-left"><?=$row['email']?></td>
                        <td class="text-right">
                            <?php switch($row['role']){
                                case '1':
                                    echo "<span style='color: #009ff1'>电商客户</span>";
                                    break;
                                case '2':
                                    echo "<span style='color: #6fb420'>品牌客户</span>";
                                    break;
                                case '100':
                                    echo "<span style='color: #ff33ff'>管理员</span>";
                                    break;
                                default:
                                    echo "<span style='color: #ff0000'>未设置</span>";
                                    break;
                            }?>
                        </td>
                        <td class="text-right"><?=$row['company']?></td>
                        <td class="text-right">
                            <a class="btn btn-oms btn-sm" href="javascript:void(0)" onclick="SetAccount(<?=$row['id']?>)">设置</a>
                        </td>
                    </tr>
                    <?php endforeach; else:?>
                        <tr><td colspan="6" class="emptyNumber"><?=lang('public_mygdsj')?>....</td></tr>
                    <?php endif; ?>
                </tbody>
                </table>
                <?php if($pageShow):?>
                <div class="page">
                    <?=$pageShow?>
                </div>
                <?php endif;?>
            </div>
          </div>
        </div>
    </div>
  
    <div style="display: none" id="infoView">
        <div class="ct_content" style="margin-top: 10px;margin-left: 20px;margin-bottom: 20px;" id="infoData">
            <form action="<?=blang_url('users/member/SetAccount')?>" method="post">
            <table cellspacing="0" style="border-collapse: collapse;width: 860px;">
                <tr>
                    <th width="21%" class="leftTd">账户名：</th>
                    <td id="name" width="79%" style="color: #ff0000;font-size: 20px;"></td>
                </tr>
                <tr>
                    <th>邮箱：</th>
                    <td id="emailTd"><span id="email"></span></td>
                </tr>
                <tr>
                    <th>公司：</th>
                    <td id="companyTd"><span id="company"></span></td>
                </tr>
                <tr>
                    <th class="leftTd">账户类型：</th>
                    <td id="roleTd">
                    </td>
                </tr>
                <tr>
                    <th class="leftTd">关联店铺：</th>
                    <td id="branch">

                    </td>
                </tr>
                <tr>
                    <th class="leftTd">支付/充值/店铺：</th>
                    <td>
                        <select id="pay_shop_id" name="pay_shop_id" style="width: 300px;">
                        </select>
                    </td>
                </tr>
                <tr>
                    <th class="leftTd">权限：</th>
                    <td id="permission">
                        <input type="checkbox" name="permission[]"  value="1"> Place Orders
                        <input type="checkbox" name="permission[]"  value="2"> Tickets
                        <input type="checkbox" name="permission[]"  value="3"> Messages
                        <input type="checkbox" name="permission[]"  value="4"> Orders
                        <input type="checkbox" name="permission[]"  value="5"> Deposit
                        <input type="checkbox" name="permission[]"  value="6"> Products
                        <input type="checkbox" name="permission[]"  value="7"> dash_batch_orders
                    </td>
                </tr>
                <tr align="center">
                    <th colspan="2">
                        <input type="hidden" name="tr_id" id="tr_id">
                        <input type="hidden" name="id" id="id">
                        <input type="submit" value=" 提 交 " onclick="return Sub()" style="width: 200px;">
                    </th>
                </tr>
            </table>
            </form>
        </div>
    </div>

</div>